package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.po.MarketKeyword;
import com.cskaoyan.bean.po.MarketKeywordExample;
import com.cskaoyan.bean.po.MarketSearchHistory;
import com.cskaoyan.bean.po.MarketSearchHistoryExample;
import com.cskaoyan.bean.chaosbean.search.vo.IndexVO;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.service.adminservice.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author jackX
 * @datetime 2023/1/2 8:45
 * @description: TODO:
 */
@Component
public class SearchServiceImpl implements SearchService {

    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;
    @Autowired
    MarketKeywordMapper keywordMapper;

    @Override
    public void addSearchHistory(MarketSearchHistory searchHistory) {
        searchHistory.setAddTime(new Date());
        searchHistory.setUpdateTime(new Date());
        searchHistory.setDeleted(false);
        searchHistory.setFrom("wx");
        searchHistory.setUserId(1);
        searchHistoryMapper.insertSelective(searchHistory);
    }

    @Override
    public IndexVO indexSearch() {
        IndexVO vo = new IndexVO();
        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andIsDefaultEqualTo(true);
        List<MarketKeyword> Default = keywordMapper.selectByExample(example);
        MarketKeywordExample example1 = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andIsHotEqualTo(false);
        List<MarketKeyword> hot = keywordMapper.selectByExample(example1);
        MarketSearchHistoryExample example2 = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andUserIdEqualTo(1);
        criteria2.andDeletedEqualTo(false);
        List<MarketSearchHistory> histories = searchHistoryMapper.selectByExample(example2);
        vo.setDefaultKeyword(Default);
        vo.setHotKeywordList(hot);
        vo.setHistoryKeywordList(histories);
        return vo;
    }

    @Override
    public void clearHistory() {
        MarketSearchHistory history = new MarketSearchHistory();
        history.setDeleted(true);
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        searchHistoryMapper.updateByExampleSelective(history,example);
    }
}
